package com.video4s.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.video4s.entity.VideoEntity;
import com.video4s.utils.Utils;

public class VideoImplDAO extends BaseDAO implements VideoDAO {

//	private final static String INSERT = "INSERT INTO Video(UserYoutubeId,VideoName,VideoImage,VideoFile," +
//			"VideoDescription,VideoContent,VideoResource,VideoMetaTitle,VideoMetaDescription,VideoMetaKeyword," +
//			"VideoUrlFriendly) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
	private final static String INSERT = "EXEC INSERT_VIDEO_ROBOTS ?,?,?,?,?,?,?,?,?,?,?,?";
	
	private final static String SELECT_ONE = "SELECT * FROM Video WHERE VideoResource = ?";
	
	@Override
	public void onInsert(VideoEntity videoEntity) throws Exception {
		// TODO Auto-generated method stub
		try {
			//if(!onSelectOne(videoEntity.getVideoFile())) {
				getConnection();
				PreparedStatement preparedStatement2 = conn.prepareStatement(INSERT);
				preparedStatement2.setInt(1, videoEntity.getCategoryId());
				preparedStatement2.setString(2, videoEntity.getUserYoutubeId());
				preparedStatement2.setString(3, videoEntity.getVideoTitle());
				preparedStatement2.setString(4, videoEntity.getVideoImage());
				preparedStatement2.setString(5, videoEntity.getVideoFile());
				preparedStatement2.setString(6, videoEntity.getVideoDescription());
				preparedStatement2.setString(7, "");
				preparedStatement2.setString(8, videoEntity.getVideoFile());
				preparedStatement2.setString(9, videoEntity.getVideoTitle());
				preparedStatement2.setString(10, videoEntity.getVideoDescription());
				preparedStatement2.setString(11, "");
				preparedStatement2.setString(12, Utils.toUrlFriendly(videoEntity.getVideoTitle()));
				preparedStatement2.executeUpdate();
				preparedStatement2.close();
			//}
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
	}

	@Override
	public void onDelete(VideoEntity videoEntity) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSelect(VideoEntity videoEntity) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSelectBy(VideoEntity videoEntity) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSelectOne(String videoResource) throws Exception {
		try {
			getConnection();
			PreparedStatement pStmt1 = conn.prepareStatement(SELECT_ONE);
			pStmt1.setString(1, videoResource);
			ResultSet resultSet = pStmt1.executeQuery();
			if(resultSet.next()) {
				return true;
			}
			resultSet.close();
			pStmt1.close();
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
		return false;
	}

	

}
